#pragma once

#include "resource.h"


class CEchoDlg : public CDialogImpl<CEchoDlg>, public CWinDataExchange<CEchoDlg>
{
private:
	TCHAR str[20];

	CTrackBarCtrl sliderWetDryMix;
	CTrackBarCtrl sliderFeedBack;
	CTrackBarCtrl sliderDelay;

	FXECHO_PARAMETERS& Parameters;
	FXECHO_PARAMETERS CurrentParameters;
public:
	enum { IDD = IDD_ECHO };

	CEchoDlg(FXECHO_PARAMETERS& Param) : Parameters(Param) {} ;

	BEGIN_MSG_MAP(CEchoDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		COMMAND_ID_HANDLER(IDOK, OnOkCloseCmd)
		COMMAND_ID_HANDLER(IDCANCEL, OnCloseCmd)
		NOTIFY_HANDLER(IDC_SLIDER_WETDRYMIX, NM_CUSTOMDRAW, OnNMCustomDrawSliderWetDryMix)
		NOTIFY_HANDLER(IDC_SLIDER_FEEDBACK, NM_CUSTOMDRAW, OnNMCustomDrawSliderFeedBack)
		NOTIFY_HANDLER(IDC_SLIDER_DELAY, NM_CUSTOMDRAW, OnNMCustomDrawSliderDelay)
	END_MSG_MAP()

	BEGIN_DDX_MAP()
		DDX_TEXT(IDC_EDIT_WETDRYMIX, str)
		DDX_TEXT(IDC_EDIT_FEEDBACK, str)
		DDX_TEXT(IDC_EDIT_DELAY, str)
		
	END_DDX_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		CenterWindow(GetParent());

		sliderWetDryMix.Attach(GetDlgItem(IDC_SLIDER_WETDRYMIX));
		sliderWetDryMix.SetRange((int)FXECHO_MIN_WETDRYMIX, (int)100 * FXECHO_MAX_WETDRYMIX,  true);
		//sliderWetDryMix.SetPos((int)100 * FXECHO_MIN_WETDRYMIX);
		if (Parameters.WetDryMix >= FXECHO_MIN_WETDRYMIX && Parameters.WetDryMix <= FXECHO_MAX_WETDRYMIX){
			sliderWetDryMix.SetPos((int)100 * (Parameters.WetDryMix));
			CurrentParameters.WetDryMix = Parameters.WetDryMix;
		} else {
			sliderWetDryMix.SetPos((int)100 * FXECHO_DEFAULT_WETDRYMIX);
			CurrentParameters.WetDryMix = FXECHO_DEFAULT_WETDRYMIX;
		}

		sliderFeedBack.Attach(GetDlgItem(IDC_SLIDER_FEEDBACK));
		sliderFeedBack.SetRange((int)100 * FXECHO_MIN_FEEDBACK, (int)100 * FXECHO_MAX_FEEDBACK,  true);
		//sliderFeedBack.SetPos((int) 100 * FXECHO_DEFAULT_FEEDBACK);
		if (Parameters.Feedback >= FXECHO_MIN_FEEDBACK && Parameters.Feedback <= FXECHO_MAX_FEEDBACK){
			sliderFeedBack.SetPos((int)100 * (Parameters.Feedback) );
			CurrentParameters.Feedback = Parameters.Feedback;
		} else {
			sliderFeedBack.SetPos((int)100 * FXECHO_DEFAULT_FEEDBACK);
			CurrentParameters.Feedback = FXECHO_DEFAULT_FEEDBACK;
		}



		sliderDelay.Attach(GetDlgItem(IDC_SLIDER_DELAY));
		sliderDelay.SetRange((int) FXECHO_MIN_DELAY, (int) FXECHO_MAX_DELAY,  true);
		//sliderDelay.SetPos((int)  FXECHO_DEFAULT_DELAY);
		if (Parameters.Delay >= FXECHO_MIN_DELAY && Parameters.Delay <= FXECHO_MAX_DELAY){
			sliderDelay.SetPos((int) (Parameters.Delay) );
			CurrentParameters.Delay = Parameters.Delay;
		} else {
			sliderDelay.SetPos((int) FXECHO_DEFAULT_DELAY);
			CurrentParameters.Delay = FXECHO_DEFAULT_DELAY;
		}

		return TRUE;
	}

	LRESULT OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		EndDialog(wID);
		return 0;
	}

	LRESULT OnOkCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		Parameters = CurrentParameters;
		EndDialog(wID);
		return 0;
	}
	
	LRESULT OnNMCustomDrawSliderWetDryMix(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& /*bHandled*/)
	{		
		int nVal = sliderWetDryMix.GetPos();
		swprintf(str, L"%.2f", (float)nVal/100);
		DoDataExchange(DDX_LOAD, IDC_EDIT_WETDRYMIX);
		CurrentParameters.WetDryMix = (float)nVal/100;
		return 0;
	}

	LRESULT OnNMCustomDrawSliderFeedBack(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& /*bHandled*/)
	{		
		int nVal = sliderFeedBack.GetPos();
		swprintf(str, L"%.2f", (float)nVal/100);
		DoDataExchange(DDX_LOAD, IDC_EDIT_FEEDBACK);
		CurrentParameters.Feedback = (float)nVal/100;
		return 0;
	}

	LRESULT OnNMCustomDrawSliderDelay(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& /*bHandled*/)
	{		
		int nVal = sliderDelay.GetPos();
		swprintf(str, L"%d", nVal);
		DoDataExchange(DDX_LOAD, IDC_EDIT_DELAY);
		CurrentParameters.Delay = (float)nVal;
		return 0;
	}
};
